package net.sxnic.ugr.dept.util;

import java.util.List;

import net.sxnic.comm.basecode.BaseCode;
import net.sxnic.ugr.dept.Dept;
import net.sxnic.ugr.dept.DeptManager;
import net.sxnic.ugr.user.User;

import org.apache.commons.lang.StringUtils;

public class DeptUtils {
	
	 
	public static String createCheckboxDutyFlagHtml(String Dutyflags,
			List<BaseCode> allBasecode, String DutyFlag) {

		StringBuffer sb = new StringBuffer();
		 
        int i=0;
        String lstrbr="";
		for (BaseCode basecode : allBasecode) {
			i++;
			lstrbr=i%2==0?"<br>":"";
			if (StringUtils.contains(Dutyflags, basecode.getInfoCode())) {
				sb.append("<input type='checkbox' name='" + DutyFlag + "' value='"
						+ basecode.getInfoCode() + "' checked='true' />&nbsp;"
						+ basecode.getInfoCode()+ basecode.getInfoName() +","+ "&nbsp;"+lstrbr);
			} else {
				sb.append("<input type='checkbox' name='" + DutyFlag + "' value='"
						+ basecode.getInfoCode()  + "' />&nbsp;" + basecode.getInfoCode()+ basecode.getInfoName()+"," + "&nbsp;"+lstrbr);
			}
		}

		return sb.toString();

	}
	
	public static String createCheckboxHtml(List<Dept> depts,
			List<Dept> alldepts, String name) {

		StringBuffer sb = new StringBuffer();

		String temp = ",";
		for (Dept d : depts) {
			temp = temp + d.getCode() + ",";
		}
		
        int i=0;
        String lstrbr="";
		for (Dept p : alldepts) {
			i++;
			lstrbr=i%5==0?"<br>":"";
			if (StringUtils.contains(temp, ","+p.getCode()+",")) {
				sb.append("<input type='checkbox' name='" + name + "' value='"
						+p.getCode()+ "' checked='true' />"
						+p.getName()+ "&nbsp;"+lstrbr);
			} else {
				sb.append("<input type='checkbox' name='" + name + "' value='"
						+p.getCode()+ "' />" + p.getName()+ "&nbsp;"+lstrbr);
			}
		}
		return sb.toString();

	}
	
	/**
	 * 根据zTree创建的组织机构-用户的树形菜单
	 * @param deptManager
	 * @return
	 */
	public static String crtDeptUserZTreeHtml(DeptManager deptManager,String suffixUrl) {
		StringBuffer sb = new StringBuffer();

		sb.append("var zNodes =[");

		//遍历所有最顶级的组织机构
		List<Dept> firstLevelDepts = deptManager.getTopList();
		
		for(Dept d:firstLevelDepts){
			sb.append(crtDeptsTreeHtml(d,"0", suffixUrl));
		}
		
		return StringUtils.removeEnd(sb.toString(), ",")+"]";
	}

	/**
	 * 根据zTree创建的组织机构-用户的树形菜单的Html
	 * @param dept
	 * @param parentId
	 * @return
	 */
	public static String crtDeptsTreeHtml(Dept dept,String parentId,String suffixUrl) {
	
		//parentId最顶层节点的parentId=0
		int id= 0;
		StringBuilder sb = new StringBuilder();
		
		if("0".equals(parentId)){
			id = Integer.parseInt(dept.getCode());
		}else{
			id = Integer.parseInt(StringUtils.substring(dept.getCode(), 0,dept.getCode().length()));
		}
				
		sb.append("{ id:" + id + ",pId:" + Integer.parseInt(parentId)
				+ ",name:'" + dept.getName() + "',url:'" + suffixUrl
				+ "?deptCode=" + dept.getCode() + "',target:'stat_frame'},");
		
		for(Dept d:dept.getChild()){
			sb.append(crtDeptsTreeHtml(d, dept.getCode(),suffixUrl));
		}
		
		for(User u:dept.getUsers()){
			sb.append("{ id:" + u.getUserOrder() + ",pId:"
					+ Integer.parseInt(dept.getCode()) + ",name:'"
					+ u.getFullname() + "',url:'" + suffixUrl + "?userName="+u.getUsername()+"',target:'stat_frame'},");
		}
		
		return sb.toString();
	}

}
